package com.loongtech.bi.manager.game;

import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.NumberUtil;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
 * 商城统计
 */
@Service
public class MallStatisticsManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private MallStatistics10LevelManager mallStatistics10LevelManager;
	@Autowired
	private MallStatistics10LevelNoParamManager mallStatistics10LevelNoParamManager;
	@Autowired
	private MallStatisticsAllManager mallStatisticsAllManager;
	@Autowired
	private MallStatisticsAllNoParamManager mallStatisticsAllNoParamManager;
	//统计
	public List<MallStatistics> getMallStatistics(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer,String param,Integer roleLevel,String currencytype, String reason) {
		List<MallStatistics> result = new ArrayList<MallStatistics>();
		
		List<Object[]> mallStatistics10LevelList = new ArrayList<Object[]>();
		List<Object[]> mallStatisticsAllList = new ArrayList<Object[]>();
		
		//无筛选
		if(StringUtil.isBlank(param) && (roleLevel == -1)) {
			mallStatistics10LevelList = mallStatistics10LevelManager.getMallStatistics10LevelByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,currencytype, reason);
			mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, currencytype, reason);
		}//总体
		else if(StringUtil.isBlank(param) && (roleLevel == -2)) {
			mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, currencytype, reason);
		}//选择等级了
		else if(StringUtil.isBlank(param) && (roleLevel != -1) && (roleLevel != -2)) {
			mallStatistics10LevelList = mallStatistics10LevelManager.getMallStatistics10LevelByAllKindByRolelevel(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,currencytype,roleLevel, reason);
		}//传入物品id
		else if(!StringUtil.isBlank(param) && (roleLevel == -1)) {
			mallStatistics10LevelList = mallStatistics10LevelManager.getMallStatistics10LevelByAllKindByParam(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,currencytype,NumberUtil.parseLong(param), reason);
			mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKindByParam(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, currencytype, NumberUtil.parseLong(param), reason);
		}//传入物品id
		else if(!StringUtil.isBlank(param) && (roleLevel == -2)) {
			mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKindByParam(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, currencytype, NumberUtil.parseLong(param), reason);
		}
		//传入物品id
		else if(!StringUtil.isBlank(param) && (roleLevel != -1) && (roleLevel != -2)) {
			mallStatistics10LevelList = mallStatistics10LevelManager.getMallStatistics10LevelByAllKindByParamAndRolelevel(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,currencytype,NumberUtil.parseLong(param),roleLevel, reason);
		}
		
		//10等级的
		for(Object[] o : mallStatistics10LevelList) {
			MallStatistics mallStatistics = new MallStatistics();
			String day = o[0].toString();
			Integer rolelevel = NumberUtil.parseInt(o[1]);
			mallStatistics.setDay(day);
			mallStatistics.setRolelevel(rolelevel);
			mallStatistics.setParam(NumberUtil.parseLong(o[2]));
			mallStatistics.setSumchangenum(NumberUtil.parseLong(o[3]));
			mallStatistics.setTimes(NumberUtil.parseInt(o[4]));
			mallStatistics.setRolenum(NumberUtil.parseInt(o[5]));
			mallStatistics.setCurrencytype(NumberUtil.parseInt(o[6]));
			mallStatistics.setReason(NumberUtil.parseInt(o[7]));

			//获得没有Param的数据
			List<Object[]> list = mallStatistics10LevelNoParamManager.getMallStatistics10LevelByAllKind(day, queryPlatform, queryChannel, queryServer, currencytype,rolelevel);
			
			for(Object[] o1 : list) {
				mallStatistics.setAllSumchangenum(NumberUtil.parseLong(o1[0]));
				mallStatistics.setAllRolenum(NumberUtil.parseInt(o1[1]));
			}
			
			result.add(mallStatistics);
		}
		
		//总体的
		for(Object[] o : mallStatisticsAllList) {
			MallStatistics mallStatistics = new MallStatistics();
			String day = o[0].toString();
			Integer rolelevel = NumberUtil.parseInt(o[1]);
			mallStatistics.setDay(day);
			mallStatistics.setRolelevel(rolelevel);
			mallStatistics.setParam(NumberUtil.parseLong(o[2]));
			mallStatistics.setSumchangenum(NumberUtil.parseLong(o[3]));
			mallStatistics.setTimes(NumberUtil.parseInt(o[4]));
			mallStatistics.setRolenum(NumberUtil.parseInt(o[5]));
			mallStatistics.setCurrencytype(NumberUtil.parseInt(o[6]));
			mallStatistics.setReason(NumberUtil.parseInt(o[7]));

			//获得没有Param的数据
			List<Object[]> list = mallStatisticsAllNoParamManager.getMallStatisticsAllByAllKindByReason(day, queryPlatform, queryChannel, queryServer, currencytype, reason);
			
			for(Object[] o1 : list) {
				mallStatistics.setAllSumchangenum(NumberUtil.parseLong(o1[0]));
				mallStatistics.setAllRolenum(NumberUtil.parseInt(o1[1]));
			}
			
			result.add(mallStatistics);
		}
		
		return result;
	}
	
	//统计
	public class MallStatistics {
		private String day; // 日期
		private String platformid;
		private String serverid;
		private Integer currencytype;
		private String currencytypeStr;
		private Integer reason;
		private Integer rolelevel;
		private Long param;
		private String mallName;
		private String itemName;
		private String levelName;
		private Long sumchangenum;
		private Integer times;
		private Integer rolenum;
		private Long allSumchangenum = 1l;
		private Integer allRolenum = 1;
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public String getPlatformid() {
			return platformid;
		}
		public void setPlatformid(String platformid) {
			this.platformid = platformid;
		}
		public String getServerid() {
			return serverid;
		}
		public void setServerid(String serverid) {
			this.serverid = serverid;
		}
		public Integer getCurrencytype() {
			return currencytype;
		}
		public void setCurrencytype(Integer currencytype) {
		    this.currencytype = currencytype;
		    this.currencytypeStr = (GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_SEARCH_CURRENCYTYPE).get(String.valueOf(currencytype)) == null) ? String.valueOf(currencytype) : GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_SEARCH_CURRENCYTYPE).get(String.valueOf(currencytype));
		}
		public Integer getReason() {
			return reason;
		}
		public void setReason(Integer reason) {
			this.reason = reason;
		}
		public Integer getRolelevel() {
			return rolelevel;
		}
		public void setRolelevel(Integer rolelevel) {
			this.rolelevel = rolelevel;

            if(rolelevel == -1) {
                this.levelName = "总体";
            } else {
                int beginLevel = rolelevel * 10 + 1;
                int endLevel = (rolelevel + 1) * 10;
                this.levelName =  beginLevel + "-" + endLevel + "等级";
            }
		}
		public Long getParam() {
			return param;
		}
		public void setParam(Long param) {
		    this.param = param;
		    String paramStr = String.valueOf(param);
            this.mallName = (GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME).get(paramStr) == null) ? paramStr : GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME).get(paramStr);
		    this.itemName = (GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(paramStr) == null) ? paramStr : GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(paramStr);
        }
		public Long getSumchangenum() {
			return sumchangenum;
		}
		public void setSumchangenum(Long sumchangenum) {
			this.sumchangenum = sumchangenum;
		}
		public Integer getTimes() {
			return times;
		}
		public void setTimes(Integer times) {
			this.times = times;
		}
		public Integer getRolenum() {
			return rolenum;
		}
		public void setRolenum(Integer rolenum) {
			this.rolenum = rolenum;
		}
		public Long getAllSumchangenum() {
			return allSumchangenum;
		}
		public void setAllSumchangenum(Long allSumchangenum) {
			this.allSumchangenum = allSumchangenum;
		}
		public Integer getAllRolenum() {
			return allRolenum;
		}
		public void setAllRolenum(Integer allRolenum) {
			this.allRolenum = allRolenum;
		}

        public String getMallName() {
            return mallName;
        }

        public void setMallName(String mallName) {
            this.mallName = mallName;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public String getLevelName() {
            return levelName;
        }

        public void setLevelName(String levelName) {
            this.levelName = levelName;
        }

        public String getCurrencytypeStr() {
            return currencytypeStr;
        }

        public void setCurrencytypeStr(String currencytypeStr) {
            this.currencytypeStr = currencytypeStr;
        }
    }
}
